<template>
  <h2>home</h2>
  <hr />
  <div class="layout">
    <div class="left" :style="{ backgroundColor: bgColor }" :class="{ 'l-close': coll }">
      <div class="logo" @click="$link('/home')" :class="{ logo_s: coll }">首页</div>
      <div class="menu">
        <el-menu
          class="el-menu-demo"
          :background-color="bgcolor_self"
          :text-color="txcolor_self"
          :active-text-color="ac_txcolor_self"
          :default-active="act"
          :collapse="coll"
          :unique-opened="true"
          :collapse-transition="false"
        >
          <template v-for="it in menuList">
            <el-menu-item :index="it.index" v-if="!it.child" @click="tabClick(it)"
              ><i class="el-icon-help"></i><span>{{ it.title }}</span></el-menu-item
            >
            <el-submenu :index="it.index" v-if="it.child">
              <template #title>
                <i class="el-icon-help"></i>
                <span>{{ it.title }}</span>
              </template>

              <template v-for="item in it.child">
                <el-menu-item :index="item.index" @click="tabClick(item)"
                  ><i class="el-icon-help"></i><span>{{ item.title }}</span></el-menu-item
                >
              </template>
            </el-submenu>
          </template>
        </el-menu>
      </div>
    </div>

    <div class="right" :class="{ 'r-close': coll }">
      <div class="view-head">
        <i
          @click="coll = !coll"
          :class="!coll ? 'el-icon-s-fold' : 'el-icon-s-unfold'"
        ></i
        ><span>首页</span>

        <div class="userbox">
          <div class="avatar"></div>
          <div class="userName" v-if="getInfo">{{ getInfo.userName }}</div>
          <div class="dropdown">
            <p @click="open">更换主题</p>
            <p @click="(dialogVisible = true), (t = 1)">修改密码</p>
            <p @click="(dialogVisible = true), (t = 2)">退出登录</p>
          </div>
        </div>
      </div>
      <div class="view-nav">
        <el-tabs
          v-model="act"
          type="card"
          closable="closable"
          @tab-click="clickTab"
          @tab-remove="clickRemove"
        >
          <el-tab-pane
            v-for="(item, inx) in tabList"
            :key="item.name"
            :label="item.title"
            :name="item.name"
          ></el-tab-pane>
        </el-tabs>
      </div>

      <div class="view-con">
        <router-view v-slot="{ Component }">
          <transition name="display">
            <component :is="Component" />
          </transition>
        </router-view>
        <!-- <transition name="display" >
          <router-view></router-view>
        </transition> -->
      </div>
    </div>

    <!-- 提示框 -->
    <el-dialog
      :title="t == 1 ? '修复登录密码' : '提示'"
      v-model="dialogVisible"
      width="25%"
      :close-on-click-modal="false"
      ><span v-if="t == 2">确定是否退出登录?</span>
      <el-form label-width="90px" v-else>
        <el-form-item label="原密码">
          <el-input
            v-model="option.lodpwd"
            placeholder="请输入原密码"
            clearable="clearable"
            type="password"
            show-password="show-password"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input
            v-model="option.newpwd"
            placeholder="请输入新密码"
            clearable="clearable"
            type="password"
            show-password="show-password"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码">
          <el-input
            v-model="option.confirmpwd"
            placeholder="请确认新密码"
            clearable="clearable"
            type="password"
            show-password="show-password"
          ></el-input>
        </el-form-item>
      </el-form>
      <span class="dialog-footer" slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            dialogVisible = false;
            back_login();
          "
          v-if="t == 2"
          >确 定</el-button
        >
        <el-button type="primary" @click="confirm" v-else>确 定</el-button>
      </span>
    </el-dialog>
  </div>

  <hr />
  <router-view></router-view>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'

import { mapState, mapMutations, mapAction, mapGetters } from "vuex";
import { post, get, setItem, getItem, remItem } from "../utils/com";
export default {
  name: "Home",
  setup(props) {
    const menuList = getItem("menuList") ? getItem("menuList") : [];
    return {
      menuList,
    };
  },
  data() {
    return {
      bgcolor_self: "#6c7892",
      txcolor_self: "#eff1f3",
      ac_txcolor_self: "#bc0010",
      // bgColor: "#bfcee8",
      coll: false,
      // menuList: [
      //   {
      //     index: "home",
      //     title: "首页",
      //     path: "home",
      //   },
      //   {
      //     index: "heart",
      //     title: "心血管疾病",
      //     child: [
      //       {
      //         index: "heart/heart1",
      //         title: "心血管疾病 1",
      //         path: "heart/heart1",
      //       },
      //       {
      //         index: "heart/heart2",
      //         title: "心血管疾病 2",
      //         path: "heart/heart2",
      //       },
      //     ],
      //   },
      //   {
      //     index: "diabetes",
      //     title: "糖尿病",
      //     child: [
      //       {
      //         index: "diabetes/diabetes1",
      //         title: "糖尿病1",
      //         path: "diabetes/diabetes1",
      //       },
      //       {
      //         index: "diabetes/diabetes2",
      //         title: "糖尿病2",
      //         path: "diabetes/diabetes2",
      //       },
      //     ],
      //   },
      //   {
      //     index: "grop",
      //     title: "表组",
      //     child: [
      //       {
      //         index: "grop/grop1",
      //         title: "表组1",
      //         path: "grop/grop1",
      //       },
      //       {
      //         index: "grop/grop2",
      //         title: "表组2",
      //         path: "grop/grop2",
      //       },
      //     ],
      //   },
      // ],
      // menuList: this.$getItem("menuList") ? this.$getItem("menuList") : [],
      act: "/home",
      tabList: this.$getItem("tabList") ? this.$getItem("tabList") : [],
      routePath: this.$route.path,
      dialogVisible: false, // 是否显示头像交互对话框
      // dialogVisible: true, // 是否显示头像交互对话框
      t: 1,
      option: {
        lodpwd: "",
        newpwd: "",
        confirmpwd: "",
      },
    };
  },
  mounted() {
    // this.$route.push("/home");
    // this.menuList = this.$getItem("menuList") ? this.$getItem("menuList") : []
    this.filterMenu();
    if (!localStorage.getItem("tabList")) {
      this.tabList = [
        {
          title: this.$route.name,
          name: this.$route.path.substring(1, this.$route.path.length),
        },
      ];
      this.$setItem("tabList", this.tabList);
    }
  },
  methods: {
    back_login() {
      this.$notify.success({
        title: "通知",
        message: "退出成功！",
        showClose: false,
        duration: 1000,
      });
      setTimeout(() => {
        this.$store.dispatch("clearAll");
      }, 1000);
    },
    open() {
      this.$notify.success({
        title: "通知",
        message: "暂未开发更换主题功能，请等待后续开发",
        showClose: false,
        duration: 1000,
      });
    },
    tabClick(it) {
      // 添加记录
      this.$link("/" + it.path);
      this.tabList.push({
        title: it.title,
        name: it.path,
      });
      this.tabList = this.sort(this.tabList);
      this.$setItem("tabList", this.tabList);
    },
    filterMenu() {
      this.act = this.routePath.substring(1, this.routePath.length);
    },
    clickTab(targetName) {
      // 路由跳转
      // console.log(targetName);
      // console.log(targetName.props);
      // console.log(targetName.props.name);
      this.$link("/" + targetName.props.name);
    },
    clickRemove(targetName) {
      // 删除组件
      let el = this.tabList.length;
      this.tabList.forEach((item, inx) => {
        if (item.name == targetName && inx > 0) {
          if (this.act == targetName) {
            this.act = this.tabList[inx - 1].name;
            this.$link("/" + this.act);
          }
          this.tabList.splice(inx, 1);
          this.$setItem("tabList", this.tabList);
        }
      });
    },
    // 对象去重
    sort(arr) {
      let temp = arr.map((item) => {
        return JSON.stringify(item);
      });
      temp = Array.from(new Set(temp));
      return temp.map((item) => {
        return JSON.parse(item);
      });
    },
    confirm() {
      alert("医疗数据较为重要，尚未支持用户修改密码！");
    },
  },
  computed: {
    ...mapGetters(["bgColor", "getInfo"]),
  },
  watch: {
    $route(to, from, next) {
      this.routePath = to.path;
      this.filterMenu();
    },
  },
};
</script>

<style scoped>
.layout {
  width: 100%;
  height: 100%;
  position: relative;
}
.layout .left {
  width: 200px;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  overflow-y: auto;
  transition: all 0.3s;
}
.layout .left .logo {
  height: 50px;
  width: 140px;
  background-color: #515a6e;
  cursor: pointer;
  position: fixed;
  margin-left: 26px;
  margin-top: 8px;
  top: 0;
  text-align: center;
  line-height: 50px;
  font-size: 30px;
  color: #ddd;
}
.layout .left .logo_s {
  background-color: #515a6e;
  margin-top: 7px;
  width: 65px;
  margin-left: 0;
  font-size: 15px;
  color: #909399;
}
.layout .left .menu {
  position: fixed;
  top: 65px;
  left: 0;
  bottom: 0;
  width: 200px;
}
.layout .l-close {
  width: 65px;
}
.layout .right {
  /* background-color: #00aeff;  */
  background-color: #00aeff;
  /* 右边边框颜色 */
  height: 100%;
  transition: all 0.3s;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 200px;
}
.layout .right .view-head {
  height: 55px;
  line-height: 55px;
  /* background-color: rgb(148, 201, 245); */
  background-color: rgb(148, 201, 245);
  padding: 0 10px;
}
.layout .right .el-icon-s-fold,
.layout .right .el-icon-s-unfold {
  font-size: 30px;
  cursor: pointer;
  position: relative;
  top: 6px;
}
.layout .right .userbox {
  float: right;
  position: relative;
}
.layout .right .userbox .dropdown {
  position: absolute;
  top: 48px;
  right: 0;
  width: 116px;
  box-shadow: 0 0 10px #ddd;
  text-align: center;
  background-color: #fff;
  z-index: 99;
  cursor: pointer;
  display: none;
}
.layout .right .userbox .dropdown p {
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  color: #666;
  border-bottom: 1px solid #f2f2f2;
}
.layout .right .userbox .dropdown p:hover {
  background-color: #ecf5ff;
  color: #66b1ff;
}
.layout .right .userbox:hover .dropdown {
  display: block;
}
.layout .right .userbox .avatar {
  float: right;
  width: 40px;
  height: 40px;
  border: 2px solid #ddd;
  margin: 5px 0 0 0;
  cursor: pointer;
}
.layout .right .userbox .userName {
  float: right;
  font-size: 17px;
  margin-right: 10px;
}
.layout .right .view-nav {
  background-color: #fff;
  margin: 5px 10px;
  padding: 7px 10px;
  border-radius: 4px;
  height: 40px;
}
.layout .right .view-con {
  background-color: #fff;
  margin: 5px 10px 10px 10px;
  border-radius: 4px;
  box-shadow: 0 0 10px #ddd;
  padding: 10px;
  position: absolute;
  bottom: 0;
  right: 0;
  left: 0;
  top: 114px;
  overflow-y: auto;
}
.layout .r-close {
  left: 65px;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 100%;
  border: 4px solid #ddd;
  /* background: url("../assets/images/author.png") no-repeat 50%/100%; */
  background: url("./../assets/images/author.png") no-repeat 50%/100%;
  display: block;
  margin: 20px auto;
}
.userName {
  font-size: 20px;
  cursor: pointer;
  color: #ff0000;
  text-align: center;
}
</style>
